package br.ufc.great;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

public class PostFacebook extends Activity implements DialogListener,
		OnClickListener {

	private Facebook facebookClient;
	private Button facebookButton;
	EditText et_msg_to_post;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.postfacebook);// my layout xml

		facebookButton = (Button) this
				.findViewById(R.id.Test_Facebook_Layout);
		et_msg_to_post = (EditText) this
				.findViewById(R.id.et_msg_to_post);
		
		facebookButton.setOnClickListener(this);
		
		System.out.println("Acabou oncreate");

	}

	@Override
	public void onComplete(Bundle values) {
System.out.println("entrou oncomplete");
		if (values.isEmpty()) {
			// "skip" clicked ?
			return;
		}

		// if facebookClient.authorize(...) was successful, this runs
		// this also runs after successful post
		// after posting, "post_id" is added to the values bundle
		// I use that to differentiate between a call from
		// faceBook.authorize(...) and a call from a successful post
		// is there a better way of doing this?
		System.out.println("antes do if contais key'");
		if (!values.containsKey("post_id")) {
			try {
				
				String msgIntent = getIntent().getExtras().getString("msgToPost");
				postOnWall(et_msg_to_post.getText().toString() + "\n\n" + msgIntent); //Pega												// API

				Dialog showSucess = new Dialog(this);
				showSucess.setTitle("Postado com Sucesso!");
				showSucess.setCancelable(true);
				showSucess.show();
				
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("exception");
				System.out.println(e.getMessage());
			}
		}
	}

	@Override
	public void onError(DialogError e) {
		System.out.println("erro onerror");
		System.out.println("Error: " + e.getMessage());
	}

	@Override
	public void onFacebookError(FacebookError e) {
		System.out.println("erro on facebookerro");
		System.out.println("Error: " + e.getMessage());
	}

	@Override
	public void onCancel() {
	}

	@Override
	public void onClick(View v) {
		System.out.println("entrou onclick");
		String app_id = "341594679197422";

		if (v == facebookButton) {
			facebookClient = new Facebook(app_id);
			// replace APP_API_ID with your own
			System.out.println("Criou facebook");
			
			facebookClient.authorize(this, new String[] {
					"publish_stream", "read_stream", "offline_access" }, this);
			
			
			
			
			System.out.println("Depois autorize");
			
		}
	}
	
    public void postOnWall(String msg) {
        Log.d("Tests", "Testing graph API wall post");
         try {
                String response = facebookClient.request("me");
                Bundle parameters = new Bundle();
                parameters.putString("message", msg);
                parameters.putString("description", "test test test");
                response = facebookClient.request("me/feed", parameters, 
                        "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || 
                        response.equals("false")) {
                   Log.v("Error", "Blank response");
               }
         } catch(Exception e) {
             e.printStackTrace();
         }
    }
}
